package com.huang.eduorder.component;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huang.eduorder.entity.Order;
import com.huang.eduorder.entity.SkillCourse;
import com.huang.eduorder.service.impl.OrderServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.TimeUnit;


@Component
public class MqConponent {

    @Autowired
    OrderServiceImpl service;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    //订单消息处理 1创建订单存库  修改数据库单量
    @RabbitListener(queues = "orderQueue")
    public void test(String msg, Channel c, Message message) throws IOException {

        if (msg != null) {
            byte[] a = message.getBody();
            ObjectMapper mapper = new ObjectMapper();
            Order order = mapper.readValue(a, Order.class);
            System.out.println("发送订单" + order.getId());
            //存入mysql
            boolean save = service.save(order);
            if (save) {
                c.basicAck(message.getMessageProperties().getDeliveryTag(), false);//  true 为ack前面所有消息
            }
        } else {
            c.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);//接收消息失败处理
        }
    }


    // 对订单截止时间 订单的判断  1 支付状态是否未1已支付  再次同步数据库  2 未支付 删除订单
    @RabbitListener(queues = "canalQueue")
    public void test2(String msg, Channel c, Message message) throws IOException {
        if (msg != null) {
            //通过 order id 查存在改id（可能不存在的原因有，用户手动取消订单） 并且 状态已支付  同步数据库
            byte[] a = message.getBody();
            ObjectMapper mapper = new ObjectMapper();
            Order order = mapper.readValue(a, Order.class);
            System.out.println("延时消息" + order.getId());
            // 查库  是否已经购买若没有购买  停止订单删除订单  订单失效  还原库存redis
            QueryWrapper<Order> wrapper = new QueryWrapper<>();
            wrapper.eq("id", order.getId());
            Order one = service.getOne(wrapper);
            if (one.getStatus() != 1) {
                // 用户规定时间未支付  删除该订单  恢复redis的库存量
                service.remove(wrapper);
                String key=CourseContant.PRODUCT_KILL+order.getCourseId();
                // 拿取redis的课程 进行加库存
                SkillCourse courseOrder = (SkillCourse) redisTemplate.opsForValue().get(key);
                courseOrder.setCount(courseOrder.getCount()+1);
                redisTemplate.opsForValue().set(key,courseOrder,
                        redisTemplate.opsForValue().getOperations().getExpire(key), TimeUnit.SECONDS);
            }
            c.basicAck(message.getMessageProperties().getDeliveryTag(), false);//  true 为ack前面所有消息
        } else {
            c.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);//接收消息失败处理
        }


    }
}